import * as type from './mutations-type'
import service from "@/service/index";
import Vue from 'vue'
let parentNameArr = []

export const setAllMenu = ({
    commit
}, item) => {
    // comsole.log(item)
    commit(type.ALL_MENU, item);
};
export const setAllDepatement = ({
    commit
}) => {
    getAllDepateMent(res => {
        console.log(res,'0')
        commit(type.ALL_DEPATEMENT, res);
    })

};
export const setMenuItem = ({
    commit
}, item) => {
    commit(type.MENU_ITEM, item);
};
export const setNavName = ({
    commit
}, item) => {
    commit(type.NAV_ITEM, item);
};


export const setRolesList = ({
    commit
}, item) => {
    getAllRolesList(res => {
        commit(type.ROLES_LIST, res)
    })
};


export const setAllAngentLists = ({
    commit
}, status) => {
    if (status === 'officel') {
        queryOfficialAgentList(res => {
            commit(type.ANGENT_LISTS, res)
        })
    } else {
        setTimeout(() => {
            getAllAngentList(res => {
                commit(type.ANGENT_LISTS, res)
            })
        }, 400);
      
    }

};


//获取全部的部门 
async function getAllDepateMent(cb) {
    let result = await service.haiheiServerDepateMentList();
    if (result.success) {
        let list = result.result;
        let newDepArr = [];
        let newDep = []
        let menuObj = {
            parent_id: '0',
            id: 0,
            name: '重庆迅隆易科技服务有限公司',
            childList: list
        }
        newDep.push(menuObj)
        addParentName(newDep,(res=>{
            newDepArr.push(res);
        }));
       cb(newDepArr)
    } else {
        this.$message({
            type: 'error',
            message: '获取失败'
        })
    }
}
//递归导航添父节点名称
function addParentName(data,callback) {
    for (let item of data) {
        if (item.childList != null && item.childList != undefined && item.childList.length != 0) {
            parentNameArr.push({
                name: item.name,
                parent_id: item.id
            });
            if (item.parent_id == 0) {
                item.parentName = {
                    name: '重庆迅隆易科技服务有限公司',
                    parent_id: '0'
                }
            }
            callback(item);
            addParentName(item.childList,callback);
        } else {
            item.parentName = parentNameArr.find(el => {
                return el.parent_id == item.parent_id
            })
        }
       
    }
}


//获取全部的角色
// async function getAllRolesList(cb) {
//     let result = await service.haibeiQueryAllRolesList();
//     if (result.success) {
//         let list = result.result;
//         cb(list)

//     } else {
//         this.$message({
//             type: 'error',
//             message: '获取失败'
//         })
//     }
// }
// 获取全部经纪商 原始的所有经纪商（无官方）
async function getAllAngentList(cb) {
    let result = await service.haiheiAllOldAngentList();
    if (result.success) {
        let list = result.result;
        cb(list)
    } else {
        this.$message({
            type: 'error',
            message: '获取失败'
        })
    }
}

//获取官方的经纪商列表 包含官方和其他经纪商
async function queryOfficialAgentList(cb) {
    let res = await service.haiheiQueryOfficialAgentList()
    if (res.success) {
        cb(res.result)
        // this.getAngents=res.result;
    }
}